package com.kikt.animationdemo.fragment;


import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.kikt.animationdemo.R;
import com.kikt.animationdemo.ViewPagerActivity;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.view.ViewHelper;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by kikt on 2016/1/16.
 */
public class ViewpagerFragment extends Fragment {
    @Bind(R.id.iv)
    ImageView mIv;
    @Bind(R.id.vp)
    ViewPager mVp;
    @Bind(R.id.fl_container)
    FrameLayout mFlContainer;
    private BitmapDrawable drawable;
    private FrameLayout.LayoutParams params;
    protected View mRootView;

    public ViewpagerFragment() {

    }

    public ViewpagerFragment(BitmapDrawable drawable, FrameLayout.LayoutParams params) {
        this.drawable = drawable;
        this.params = params;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_viewpager, null);
        ButterKnife.bind(this, mRootView);
        Log.d("ViewpagerFragment", "run");
        if (drawable != null && params != null) {
            init();
        } else {
            mVp.setVisibility(View.VISIBLE);
            mVp.setAdapter(new ViewPagerActivity.MyAdapter(getContext()));
        }
        return mRootView;
    }

    private void init() {
        mIv.setImageDrawable(drawable);//将传入的图片设置给缩放的imageView
        mIv.setLayoutParams(params);//将传入的原图片的布局文件设置给当前的imageView
        int width = getActivity().getWindowManager().getDefaultDisplay().getWidth();//获取屏幕的宽度（目标宽度）
        mIv.measure(0, 0);//测量
        int measuredWidth = mIv.getMeasuredWidth();//获取当前imageView的宽度
        int measuredHeight = mIv.getMeasuredHeight();
        float scale = (float) width / measuredWidth;//获取需要放大/缩小的比例
        final ValueAnimator valueAnimator = new ValueAnimator().setDuration(800);//属性动画和对应的时间
        valueAnimator.setFloatValues(1, scale);//从1倍到指定的倍数
        mIv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mIv.getViewTreeObserver().removeGlobalOnLayoutListener(this);//取消当前的侦听
                final Rect r = new Rect();
                mIv.getGlobalVisibleRect(r);//获取图片的正方形属性
                final Rect r2 = new Rect();
                mRootView.getGlobalVisibleRect(r2);//父布局的方形属性
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value = (float) animation.getAnimatedValue();//获取的是属性动画的当前数值（缩放比例）
                        ViewHelper.setScaleX(mIv, value);//将缩放比例设置给ImageView的宽高
                        ViewHelper.setScaleY(mIv, value);
                        float fraction = animation.getAnimatedFraction();//获取属性动画的播放比例
                        //通过计算获得让图片平移到中心点的数值
                        ViewHelper.setTranslationX(mIv, (r2.centerX() - r.centerX()) * fraction);//平移x坐标
                        ViewHelper.setTranslationY(mIv, (r2.centerY() - r.centerY()) * fraction);//平移y坐标
                        ViewHelper.setAlpha(mFlContainer, 1 - fraction);//设置透明的百分比
                    }
                });
                valueAnimator.start();
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
